package com.cangxuan.backend.service.impl;

import com.cangxuan.backend.bean.Page;
import com.cangxuan.backend.controller.HomeController;
import com.cangxuan.backend.dao.IGoodsDao;
import com.cangxuan.backend.entity.Goods;
import com.cangxuan.backend.entity.MarketGoods;
import com.cangxuan.backend.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    IGoodsDao goodsDao;

    @Autowired
    public void setGoodsDao(IGoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public Page<Goods> page(int pageIndex, int pageSize, String name, String code) {
        Page<Goods> goodsPage = new Page<>(pageIndex, pageSize);
        goodsPage.setRecords(goodsDao.select(pageIndex, pageSize, name + "%", code));
        goodsPage.setTotal(goodsDao.selectCount(name + "%", code));
        return goodsPage;
    }

    @Override
    public List<Goods> page(int pageIndex, int pageSize) {
        return goodsDao.selectHomeCollections(pageIndex, pageSize);
    }

    @Override
    public Goods get(int id) {
        return goodsDao.selectById(id);
    }

    @Override
    public List<MarketGoods> page(
            String searchKey,
            int category, int priceSort,
            int releaseDateSort,
            Integer[] issuers,
            Integer[] series,
            Integer pageIndex,
            Integer pageSize) {

        /**
         * 获取藏品列表
         */
        List<MarketGoods> goodsList = goodsDao.selectMarketCollection(searchKey + "%", category, priceSort,
                releaseDateSort, issuers, series, pageIndex, pageSize);
        /**
         * 获取藏品的挂单数量
         */
        goodsList.forEach(item->{
            item.setCount(goodsDao.selectMarketOrderCount(item.getId()));
        });
        return goodsList;
    }

    @Override
    public List<Goods> calendar(int pageIndex, int pageSize) {
        List<Goods> goodsList = goodsDao.selectHomeCalendar(pageIndex, pageSize);
        return goodsList;
    }

}
